export default{
  getUrlParam:function(name, url) {
    var arr = url.split("?")[1].split("&");
    var request = [];
    var temp = [];
    for (var i = 0; i < arr.length; i++) {
      temp = arr[i].split("=");
      request[temp[0]] = temp[1];
    }
    return request[name] || null;
  },
  
  //格式化消息
  formatMsg:function(Msg,myName) {
    let arr = Msg.list;
    let arrs = [];
    console.log('格式化消息',arr)
    for (let i in arr) {
      let obj = {
        messageKey: arr[i].messageKey,
        sendUserId: arr[i].sendUserId,
        receiveUserId: arr[i].receiveUserId,
        isGroup:arr[i].isGroup,
        type: arr[i].type,
        content: {
          font: {
            underline: false,
            bold: false,
            italic: false,
            color: "000000",
            name: "Microsoft YaHei",
            size: 14,
          },
          sections: [
            {
              items: [
                {
                  type: arr[i].type,
                  value: JSON.stringify({
                    text: arr[i].value,
                    sendName: myName,
                    receiveName: "",
                  }),
                },
              ],
            },
          ],
          timestamp: arr[i].timestamp,
        },
      };
      arrs.push(obj);
    }
    console.log('111111111111arrs',arrs)
    return arrs;
  },

  //返回处理后的广播的fileMap数据
  backFileMap(attach) {
    console.log('返回处理后的广播的fileMap数据',attach)
    let dowList = attach.split(",") || [];
    let msgList = [];
    dowList.map((item) => {
      msgList.push({
        isDownLoad: "0",
        downLoadId: item,
      });
    });
    return JSON.stringify(msgList);
  },

  //过滤<>
  replaceTosymbol(i){
    let a = i.replace(/<[^>]+>/g,'')
    return a
  },

  //zk新增，将待办的value换成纯文本
  toRawStr(value) {
    let str = value.replace(/<br>/g,"").replace(/&lt;/g,"<").replace(/&nbsp;/g," ").replace(/&amp;/g,"&")
    return str;
  },

  // 返回 类似 2016-01-02 格式的字符串
  formatDate(year, month, day) {
    var y = year;
    var m = month;
    if (m < 10) m = "0" + m;
    var d = day;
    if (d < 10) d = "0" + d;
    return y + "-" + m + "-" + d;
  },

  //已读回执发送
  sendMessageRead(userId,messageId){
    let nowTime=new Date().getTime();
    let obj={
      head: {
        key: "",
        name: "",
        action: "1.500",
        method: "1.1.1019",
        version: "",
        time: nowTime
      },
      body: {
        userId:userId,
        messageId:messageId  
      }
    }
    return JSON.stringify(obj)
  },

  // 当前时间 年月日时分秒
  getDating() {
    let time = new Date();
    let year = time.getFullYear()
    let month = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : "0" + (time.getMonth() + 1);
    let day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate();
    let hours = time.getHours() >= 10 ? time.getHours() : '0' + time.getHours();
    let minutes = time.getMinutes() >= 10 ? time.getMinutes() : '0' + time.getMinutes();
    let seconds = time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds();
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
  },

  // 广播附件数据处理
  initBroadFile(arrList) {
    for (let i = 0; i < arrList.length; i++) {
      if (arrList[i].attachName && arrList[i].attachName != "") {
        let attachName = arrList[i].attachName.split(",");
        let attachsize = arrList[i].attachsize.split(",");
        let attach = arrList[i].attach.split(",");
        let fileMap = JSON.parse(arrList[i].fileMap);
        if (attachName.length > 0) {
          arrList[i].files = [];
          for (let j = 0; j < attachName.length; j++) {
            arrList[i].files.push({
              attachName: attachName[j],
              attachsize: attachsize[j],
              attach: attach[j],
              isUpload: fileMap[j]
            });
          }
        }
      } else {
        arrList[i].files = [];
      }
    }
    return arrList;
  },

  //zk注释，将字符中的空格，tab和换行还原
  returnBrNbsp(str) {
    str = str.replace(/\r?\n/g, "<br>");
    str = str.replace(/\t/g, "&nbsp;");
    str = str.replace(/\s/g, '&nbsp;');
    return str;
  }, 

  //生成表情图片
  createFaceImage(index) {
    return `<img src='static/image/face/${index}.gif' draggable="false" style="width:1.6em;height:1.6em;vertical-align:bottom;flex-shrink:0;">`;
  },

  //正则替换链接
  regUrl(s,type) {
    var v = s,
      t,
      arr1 = [],
      reg;
    if(type){
      reg = /(https?|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;//zk14留用
    }else{
      reg = /(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+/g
    }
    while (t = reg.exec(s)) {
      if(!type && t[0].indexOf(';')>-1){
        continue;
      }else{
        arr1.push(t[0]);
      }
    }
    if (arr1.length > 0) {
      for (let i in arr1) {
        v = v.replace(arr1[i],`<a href='javaScript:void(0)' class="httpUrl">${arr1[i]}</a>`);
      }
    }
    return v;
  },

  //处理上传文件信息
  get_2_data(data, myId,name='') {
    let form = new FormData();
    form.append("file", data,name);
    form.append("userid", myId);
    console.log(data,myId)
    return form;
  },

  //base64转文件流
  dataURItoBlob(base64Data) {
    var byteString;
    if (base64Data.split(',')[0].indexOf('base64') >= 0) {
      byteString = atob(base64Data.split(',')[1]);
    } else {
      byteString = unescape(base64Data.split(',')[1]);
    }
    var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];
    var ia = new Uint8Array(byteString.length);
    for (var i = 0, len = byteString.length; i < len; i++) {
      ia[i] = byteString.charCodeAt(i);
    }
    console.log(ia,"base64转文件流时",mimeString)
    return new Blob([ia], {
      type: mimeString
    });
  },

  htmlDecodeByRegExp(text) {
    var temp = document.createElement("div");
    //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
    temp.innerHTML = text;
    //3.最后返回这个元素的innerText或者textContent，即得到经过HTML解码的字符串了。
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
  },

  returnEncodeBr(str) {
    str = str.replace(/<br>/g, "\n");
    str = str.replace(/&nbsp;/g, "\t");
    return str;
  },

  path_replace(data) {
    return data.replace(/\//g, "\\")
  },

  // 二维码中文乱码转换
  utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
      c = str.charCodeAt(i);
      if (c >= 0x0001 && c <= 0x007f) {
        out += str.charAt(i);
      } else if (c > 0x07ff) {
        out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f));
        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
      } else {
        out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
      }
    }
    return out;
  },
  //排序
  compare() {
    return function (a, b) {
      var value1 = a.data[0].time;
      var value2 = b.data[0].time;
      return value2 - value1;
    };
  },
  // 对象转大写
  upperJSONKey(jsonObj) {
    for (var key in jsonObj) {
      jsonObj["" + key.toUpperCase() + ""] = jsonObj[key];
    }
    return jsonObj;
  }, 
}